$(document).ready(function () {
    $('#login').click(function () {
        var username = $('#account').val();
        var password = $('#password').val();
        var validator1 = window.utils.validate(username, "require");
        var validator2 = window.utils.validate(password, "require");
        if (validator1 == true && validator2 == true) {
            $.post('/user/login',
                {
                    username: username,
                    password: password
                }, function (data) {
                    if (data.id && data.userId) {
                        $.cookie('uuId', data.id, {expires: 7, path: "/"});
                        $.cookie('userId', data.userId, {expires: 7, path: "/"});
                        $.cookie('username', username, {expires: 7, path: "/"});
                        data.real_name && $.cookie('real_name', data.real_name, {expires: 7, path: "/"});
                        window.location = "/rent";
                    } else {
                        if (data.error.statusCode == 401) {
                            window.utils.toast('error', "邮箱未验证，请登录注册邮箱验证账号");
                        } else {
                            window.utils.toast('error', "登录失败,请检查用户名密码");
                        }
                    }
                }
            );
        } else {
            window.utils.toast('warning', validator1[0]);
            window.utils.toast('warning', validator2[0]);
        }
    });

    $('#register').click(function () {
        var username = $('#account').val();
        var password = $('#password').val();
        var passwordRepeat = $('#passwordRepeat').val();
        var tel = $('#tel').val();
        var email = $('#email').val();
        var validator1 = window.utils.validate(username, "require");
        var validator2 = window.utils.validate(password, "require");
        var validator3 = window.utils.validate(tel, ["require", "tel"]);
        var validator4 = window.utils.validate(email, ["require", "email"]);
        if (validator1 == true && validator2 == true && validator3 == true && validator4 == true) {
            if (password == passwordRepeat) {

                $.post('/user/register',
                    {
                        username: username,
                        password: password,
                        mobile: tel,
                        email: email
                    }, function (data) {
                        if (data.error) {
                            if (data.error.name == 'uniqueness') {
                                window.utils.toast('error', "用户名或邮箱已被注册");
                                return;
                            }
                            window.utils.toast('error', "注册失败,请稍后重试");
                            return;
                        }
                        window.utils.toast('success', "注册成功");
                    }
                );

            } else {
                window.utils.toast('warning', "密码输入不一致");
            }
        } else {
            window.utils.toast('warning', validator1[0]);
            window.utils.toast('warning', validator2[0]);
            window.utils.toast('warning', validator3[0]);
            window.utils.toast('warning', validator4[0]);
        }
    });

    $("#forgetPwd").click(function () {
        var username = prompt("请输入注册用户名");
        var email = prompt("请输入注册邮箱");
        $.get("/user/resetPassword", {username: username, email: email}, function (data) {
            if(data == "success") {
                window.utils.toast("success","密码重置请求成功,请在邮箱查看");
            } else {
                window.utils.toast("error","重置请求失败，请检查输入");
            }
        });
    });
});